<?php
/**
 * 创客报单输入数据验证类
 * @author liujun
 */
namespace App\System\Cis\Services;

use App\System\Base\Model\BaseSetting;
use App\System\Base\Repositories\BaseSettingRepository;
use App\System\Cis\Repositories\CisUserRepository;

class DeclarationInputValidationService
{
    /**
     * 城市合伙人ID
     * @var
     */
    public $cityPartnerId;

    /**
     * 推荐人ID
     * @var
     */
    public $recommendId;

    /**
     * 账号所属者主账号手机号
     * @var
     */
    public $accountOwnerMobile;

    /**
     * 报单数量
     * @var
     */
    public $generatingNumber;

    /**
     * 滑落市场 1创客区 2创新区 3创客区
     * @var
     */
    public $market;

    /**
     * 推荐人信息
     * @var
     */
    private $recommendInfo;

    /**
     * 城市合伙人信息
     * @var
     */
    private $cityPartnerInfo;

    /**
     * 账号所属者信息
     * @var
     */
    private $accountOwnerInfo;

    /**
     * 错误信息
     * @var
     */
    private $errorMsg;

    /**
     * 用户模型对像
     * @var
     */
    private $userObj;

    /**
     * 城市合伙人报单每单价格
     */
    const UNIT_PRICE = 1000;

    /**
     * 基本参数验证
     * @return boolean
     */
    private function parameterVerification()
    {
        $keys = get_object_vars($this);
        $check_key = array('cityPartnerId','recommendId','accountOwnerMobile','generatingNumber','market');
        foreach ($keys as $k=>$v)
        {
            if(in_array($k,$check_key))
            {
                if(!$v || !is_numeric($v))
                {//验证值是否大于0
                    return $this->errorMsg = '参数值为空或类型错误';
                }
            }
        }

        if($this->generatingNumber > 6 && $this->generatingNumber != 13)
        {
            return $this->errorMsg = '报单数量不正确';
        }
        if($this->recommendId == 1)
        {
            return $this->errorMsg = '推荐人ID不能为【1】';
        }
        if($this->cityPartnerId == 1)
        {
            return $this->errorMsg = '城市合伙人ID不能为【1】';
        }

    }

    /**
     * 验证所有的用户信息
     * @return $this
     */
    public function userInformationVerification()
    {
        $this->parameterVerification();
        if($this->errorMsg) return $this->errorMsg;
        $this->userObj = CisUserRepository::getInstance();
        $this->verificationAmount();
        $this->queryRecommendationInformation();
        if($this->errorMsg) return $this->errorMsg;
        $this->queryPersonalAccount();
        return $this;
    }

    /**
     * 查询城市合伙人信息
     */
    private function queryPartnerInformation()
    {

        $this->cityPartnerInfo = $this->userObj->getUser($this->cityPartnerId,2);
        if(!$this->cityPartnerInfo)
        {
            return $this->errorMsg = '城市合伙人不存在';
        }
    }

    /**
     * 查询推荐人信息
     */
    private function queryRecommendationInformation()
    {
        $this->recommendInfo = $this->userObj->getUser($this->recommendId);
        if(!$this->recommendInfo)
        {
            return $this->errorMsg = '推荐人不存在';
        }
        $setting = BaseSettingRepository::getInstance();
        $data = $setting->getSetting();
        if($this->market ==3 && ($this->recommendInfo->entrepreneurship_achievement < $data[61] || $this->recommendInfo->chuangheng_achievement <$data[62] ))
        {
            return $this->errorMsg = "推荐人创业区或创新区分别未达到{$data[61]},{$data[62]}业绩";
        }
    }

    /**
     * 查询帐号所属人信息
     */
    private function queryPersonalAccount()
    {
        $this->accountOwnerInfo = $this->userObj->getMasterUser($this->accountOwnerMobile);
    }

    /**
     * 验证城市合伙人金额是否足够支付
     */
    private function verificationAmount()
    {
        $this->queryPartnerInformation();
        if($this->errorMsg) return $this->errorMsg;
        $totalConsumerRegisteredCurrency = $this->generatingNumber * self::UNIT_PRICE;//总消费注册币及积分
        if($this->cityPartnerInfo->integral < $totalConsumerRegisteredCurrency/2)
        {//判断是否满足最低积分消费
            return $this->errorMsg = '城市合伙人积分不足此次支付';
        }

        if($this->cityPartnerInfo->registered_currency + $this->cityPartnerInfo->integral < $totalConsumerRegisteredCurrency)
        {//判断积分加注册币是否小于总消费
            return $this->errorMsg = '城市合伙人注册币不足此次支付';
        }
    }

    /**
     * 获取错误信息
     */
    public function getErrorMsg()
    {
        return $this->errorMsg;
    }

    /**
     * 获取城市合伙人信息
     */
    public function getPartnerInformation()
    {
        return $this->cityPartnerInfo;
    }

    /**
     * 获取推荐人信息
     */
    public function getRecommendationInformation()
    {
        return $this->recommendInfo;
    }

    /**
     * 获取帐号所属人信息
     */
    public function getPersonalAccount()
    {
        return $this->accountOwnerInfo;
    }
}